﻿using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.Scripting.Ast;
using Microsoft.Scripting;
using Microsoft.Linq.Expressions;

namespace DlrBasic.Parser.Ast
{
    class Gosub : Statement
    {
        LineNumberTarget target;

        public Gosub(SourceSpan span, LineNumberTarget target)
            : base(span)
        {
            this.target = target;
        }

        protected internal override Expression Generate(DlrBasicGenerator generator, StateMemberGenerator stateParameter, LambdaBuilder builder)
        {
            Expression pushState = Expression.Call(stateParameter.GetGosubStackVariable(),
                typeof(Runtime.DlrBasicGosubStack).GetMethod("PushState"),
                Expression.Constant(generator.GetNextStatementNumber(this)));

            int state = generator.GetLineStatementNumber(target.LineNumber);

            return Expression.Block(
                pushState,
                Utils.Assign(stateParameter.GetCurrentStatementVariable(), 
                    Expression.Constant(state), target.SourceSpan));
        }
    }
}
